
//=========================================================================================================
double getAcc(char axis)
{
	double acc = -9999;
	if (axis == 'x') 
	{
		float xVoltage     	= analogRead(ACCELERATION_PIN_X) * ARDUINO_POWER_SUPPLY / 1024;
		float xAcceleration = (xVoltage - ZERO_G_BIAS) / SENSITIVITY;
		acc = xAcceleration + ACCELERATION_X_OFFSET;
	} else if (axis == 'y') 
	{
	    float yVoltage     	= analogRead(ACCELERATION_PIN_Y) * ARDUINO_POWER_SUPPLY / 1024;
		float yAcceleration = (yVoltage - ZERO_G_BIAS) / SENSITIVITY;
		acc = yAcceleration + ACCELERATION_Y_OFFSET;
	} else if (axis == 'z') 
	{
	    float zVoltage     	= analogRead(ACCELERATION_PIN_Z) * ARDUINO_POWER_SUPPLY / 1024;
		float zAcceleration = (zVoltage - ZERO_G_BIAS) / SENSITIVITY;
		acc = zAcceleration + ACCELERATION_Z_OFFSET;		
	}
		
	if (acc < -9990) return 0;

	setMinAcc(axis, acc);
	setMaxAcc(axis, acc);
	return acc;
}
//=========================================================================================================
float getRotation(char axis)
{
	float angle = 0;
	if (axis == 'y') // pitch
		angle = atan2(-getAcc('y'),-getAcc('z'))*57.2957795+180;
	
	if (axis == 'x') // roll
		angle = atan2(-getAcc('x'),-getAcc('z'))*57.2957795+180;

	if (axis == 'z') // yaw
		angle = atan2(-getAcc('y'),-getAcc('x'))*57.2957795+180;
		
	return angle;
}
//=========================================================================================================
void setMinAcc(char axis, double acc)
{
	if (axis == 'x')
		MINIMUM_ACCELERATION_X = min(MINIMUM_ACCELERATION_X, acc);
	if (axis == 'y')
		MINIMUM_ACCELERATION_Y = min(MINIMUM_ACCELERATION_Y, acc);
	if (axis == 'z')
		MINIMUM_ACCELERATION_Z = min(MINIMUM_ACCELERATION_Z, acc);
}
//=========================================================================================================
void setMaxAcc(char axis, double acc)
{
	if (axis == 'x')
		MAXIMUM_ACCELERATION_X = max(MAXIMUM_ACCELERATION_X, acc);
	if (axis == 'y')
		MAXIMUM_ACCELERATION_Y = max(MAXIMUM_ACCELERATION_Y, acc);
	if (axis == 'z')
		MAXIMUM_ACCELERATION_Z = max(MAXIMUM_ACCELERATION_Z, acc);
}
//=========================================================================================================
void accSetup()
{
	MINIMUM_ACCELERATION_X = getAcc('x');
	MINIMUM_ACCELERATION_Y =	getAcc('y');
	MINIMUM_ACCELERATION_Z = getAcc('z');
	MAXIMUM_ACCELERATION_X = getAcc('x');
	MAXIMUM_ACCELERATION_Y = getAcc('y');
	MAXIMUM_ACCELERATION_Z = getAcc('z');
}
//=========================================================================================================
